package com.aco.fatureapp.activity;

import java.util.Iterator;

import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

import com.aco.fatureapp.R;
import com.aco.fatureapp.model.Impressora;
import com.aco.fatureapp.model.Impressora.TypeFormulario;
import com.aco.fatureapp.model.Impressora.TypeImpressora;
import com.aco.fatureapp.model.Printer;
import com.aco.fatureapp.utilitary.Utils;
import com.mobsandgeeks.saripaar.annotation.Required;

@ContentView(R.layout.activity_printer)
public class PrinterActivity extends BaseActivity {

	protected static final int SELECT_DEVICE = 10;

	@InjectView(value = R.id.nomePrinter)
	@Required(order = 1, messageResId = R.string.lblNomeRequerid)
	private EditText mTxtNome;

	@InjectView(value = R.id.enderecoMac)
	@Required(order = 1, messageResId = R.string.lblEnderecoMacRequerid)
	private EditText mTxtEnderecoMac;

	@InjectView(value = R.id.flgPadrao)
	@Required(order = 1, messageResId = R.string.lblFlgPadraoRequerid)
	private CheckBox mCkFlgPadrao;

	@InjectView(value = R.id.flgDisponivel)
	@Required(order = 1, messageResId = R.string.lblFlgDisponívelRequerid)
	private CheckBox mCkFlgDisponivel;

	@InjectView(value = R.id.typeImpressora)
	@Required(order = 1, messageResId = R.string.lblTypeImpressoraRequerid)
	private Spinner mCbTypeImpressora;

	@InjectView(value = R.id.typeFormulario)
	@Required(order = 1, messageResId = R.string.lblTypeFormularioRequerid)
	private Spinner mCbTypeFormulario;

	@InjectView(value = R.id.btnFindPrinter)
	private Button mBtnFindPrinter;

	private Impressora mEntity;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.aco.fatureapp.activity.BaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mCbTypeImpressora.setAdapter(new ArrayAdapter<TypeImpressora>(this,
				R.layout.adapter_spinner, TypeImpressora.values()));

		mCbTypeFormulario.setAdapter(new ArrayAdapter<TypeFormulario>(this,
				R.layout.adapter_spinner, TypeFormulario.values()));

		final Iterator<Impressora> lstImpressora = Impressora
				.findAll(Impressora.class);

		if (lstImpressora != null && lstImpressora.hasNext()) {
			mEntity = lstImpressora.next();

			mTxtNome.setText(mEntity.getNome());
			mTxtEnderecoMac.setText(mEntity.getEnderecoMac().toString());
			mCkFlgPadrao.setChecked(mEntity.getFlgPadrao());
			mCkFlgDisponivel.setChecked(mEntity.getFlgDisponivel());
			mCbTypeImpressora.setSelection(Utils.getSpinnerIndex(
					mCbTypeImpressora, mEntity.getTypeImpressora().toString()));
			mCbTypeFormulario.setSelection(Utils.getSpinnerIndex(
					mCbTypeImpressora, mEntity.getTypeFormulario().toString()));

			mEntity.save();
		}

		mBtnFindPrinter.setOnClickListener(onClickFindBlueTooth());
	}

	private OnClickListener onClickFindBlueTooth() {
		return new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent("IF_BLUETOOTH");
				startActivityForResult(intent, SELECT_DEVICE);
			}
		};
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see roboguice.activity.RoboActivity#onActivityResult(int, int,
	 * android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == SELECT_DEVICE) {
			if (data != null) {
				Object help = data.getExtras().get(
						DeviceListActivity.VALUE_RETURN);

				if (help != null) {
					Printer printer = (Printer) help;

					mTxtNome.setText(printer.name);
					mTxtEnderecoMac.setText(printer.mac);
				}
			}
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.action_bar_menu_printer, menu);

		getActionBar().setDisplayHomeAsUpEnabled(true);

		return true;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem)
	 */
	@Override
	public boolean onMenuItemSelected(final int featureId, final MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home: {
			finish();
			return true;
		}
		case R.id.menu_salvar: {
			validator.validate();
			return true;
		}
		case R.id.menu_cancelar: {
			finish();
			return true;
		}
		}

		return true;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.aco.fatureapp.activity.BaseActivity#onValidationSucceeded()
	 */
	@Override
	public void onValidationSucceeded() {
		// Instânciar o objeto para persist.
		if (mEntity == null || mEntity.getId() == null) {
			mEntity = new Impressora();
		}

		mEntity.setNome(mTxtNome.getText().toString());
		mEntity.setEnderecoMac(mTxtEnderecoMac.getText().toString());
		mEntity.setFlgPadrao(mCkFlgPadrao.isChecked());
		mEntity.setFlgDisponivel(mCkFlgDisponivel.isChecked());
		mEntity.setTypeImpressora(TypeImpressora.valueOf(mCbTypeImpressora
				.getSelectedItem().toString()));
		mEntity.setTypeFormulario(TypeFormulario.valueOf(mCbTypeFormulario
				.getSelectedItem().toString()));

		mEntity.save();

		Utils.alert(PrinterActivity.this, "Impressora salva com sucesso!", null);
	}
}
